package com.share.ocr;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.awt.event.*;

/**
 * author:caifan
 * date:2019/10/6
 */
public class CaptureScreen {

    /* public static void main(String[] args) throws Exception {
         Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
 //
 //        Rectangle rectangle = new Rectangle();
 //
 //        Robot robot = new Robot();
 //        BufferedImage screenCapture = robot.createScreenCapture(rectangle);
         System.out.println(clipboard.getName());
     }*/
    private static final long serialVersionUID = -267804510087895906L;
    private JButton buttonCap = null;
    private JButton buttonOcr = null;
    private JTextArea textArea;
    private JLabel imgLabel = null;
    private JFrame jFrame;

    public void init() {
        jFrame = new JFrame();
        jFrame.setTitle("HelloOCR");
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Point point = new Point();
        point.setLocation(screenSize.width * 4 / 5, screenSize.height * 3 / 5);
        jFrame.setLocation(point);
        jFrame.setSize(screenSize.width / 5, screenSize.height / 4);

        JMenuBar menuBar = new JMenuBar();
        JMenu menuHelp = new JMenu("帮助");
        menuBar.add(menuHelp);

        imgLabel = new JLabel();
        imgLabel.setBackground(Color.WHITE);

        JTextArea textArea = new JTextArea(20, 40);
        JScrollPane textScrollPane = new JScrollPane();
        textScrollPane.add(textArea);

        JPanel buttonPanel = new JPanel();
        JButton buttonCapture = new JButton("截屏");
        JButton buttonFile = new JButton("读取文件");
        JButton buttonOCR = new JButton("OCR");

        DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode("百度OCR");
        JTree tree = new JTree(treeNode);

        buttonCapture.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new ScreenWindow(imgLabel, jFrame);
                } catch (Exception e1) {
                    JOptionPane.showConfirmDialog(null, "出现意外错误！", "系统提示", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        buttonPanel.add(buttonCapture);
        buttonPanel.add(buttonFile);
        buttonPanel.add(buttonOCR);

        jFrame.setLayout(new BorderLayout(20,10));
        jFrame.add(menuBar, BorderLayout.NORTH);
        imgLabel.setSize(screenSize.width / 4, screenSize.height / 4);
        jFrame.add(imgLabel, BorderLayout.CENTER);
        jFrame.add(textScrollPane, BorderLayout.EAST);
        jFrame.add(buttonPanel, BorderLayout.SOUTH);
        jFrame.add(tree, BorderLayout.WEST);

        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        jFrame.setLocationRelativeTo(null);
        jFrame.setVisible(true);

    }

    public CaptureScreen() {

        /*buttonCap = new JButton("模拟屏幕（点右键退出）");
        buttonCap.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    new ScreenWindow(imgLabel);
                } catch (Exception e1) {
                    JOptionPane.showConfirmDialog(null, "出现意外错误！", "系统提示", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        buttonOcr = new JButton("OCR文本解析");
        buttonOcr.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });
        JPanel pane = new JPanel();
        pane.setBackground(Color.WHITE);
        imgLabel = new JLabel();
        pane.add(imgLabel);
        JScrollPane spane = new JScrollPane(pane);
        this.getContentPane().add(buttonCap, BorderLayout.NORTH);
        this.getContentPane().add(buttonOcr, BorderLayout.NORTH);
        this.getContentPane().add(spane);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(600, 400);
        this.setLocationRelativeTo(null);
        this.setVisible(true);*/
    }

    public static void main(String[] args) {
        CaptureScreen captureScreen = new CaptureScreen();
        captureScreen.init();
    }
}

class ScreenWindow extends JFrame {
    private static final long serialVersionUID = -3758062802950480258L;
    private boolean isDrag = false;
    private int x = 0;
    private int y = 0;
    private int xEnd = 0;
    private int yEnd = 0;
    private CaptureScreen captureScreen = new CaptureScreen();

    public ScreenWindow(final JLabel imgLabel, JFrame parentFrame) throws AWTException, InterruptedException {
        Dimension screenDims = Toolkit.getDefaultToolkit().getScreenSize();
        JLabel label = new JLabel(new ImageIcon(ScreenImage.getScreenImage(0, 0, screenDims.width, screenDims.height)));
        label.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
        label.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON3) {
                    dispose();
                }
            }

            public void mousePressed(MouseEvent e) {
                x = e.getX();
                y = e.getY();
            }

            public void mouseReleased(MouseEvent e) {
                if (isDrag) {
                    xEnd = e.getX();
                    yEnd = e.getY();
                    if (x > xEnd) {
                        int temp = x;
                        x = xEnd;
                        xEnd = temp;
                    }
                    if (y > yEnd) {
                        int temp = y;
                        y = yEnd;
                        yEnd = temp;
                    }
                    try {
                        imgLabel.setIcon(new ImageIcon(ScreenImage.getScreenImage(x, y, xEnd - x, yEnd - y)));
                    } catch (Exception ex) {
                        JOptionPane.showConfirmDialog(null, "出现意外错误！", "系统提示", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
                    }
                    dispose();
                }
            }
        });
        label.addMouseMotionListener(new MouseMotionListener() {
            public void mouseDragged(MouseEvent e) {
                if (!isDrag)
                    isDrag = true;
            }

            public void mouseMoved(MouseEvent e) {
                /** 拖动过程的虚线选取框需自己实现 */
            }
        });
        this.setUndecorated(true);
        this.getContentPane().add(label);
        this.setSize(screenDims.width, screenDims.height);
        this.setVisible(true);
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
    }
}

class ScreenImage {
    public static Image getScreenImage(int x, int y, int w, int h) throws AWTException, InterruptedException {
        Robot robot = new Robot();
        Image screen = robot.createScreenCapture(new Rectangle(x, y, w, h)).getScaledInstance(w, h, Image.SCALE_SMOOTH);
        MediaTracker tracker = new MediaTracker(new Label());
        tracker.addImage(screen, 1);
        tracker.waitForID(0);
        return screen;
    }

}
